﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CompanyManagement.Models
{
    public class AccountDAO
    {
        CompanyEntities db;

        public AccountDAO()
        {
            db = new CompanyEntities();
        }

        public int Login(string username, string password)
        {
            var user = db.Accounts.FirstOrDefault(p=>p.username == username && p.password == password);
            if (user != null)
                return user.UserTypeID.Value;
            return -1;
        }

        internal List<Account> GetAccounts(string username)
        {
            return db.Accounts.Where(p => !p.username.Equals(username) && p.IsActive == true).ToList();
        }

        internal bool ResetPassword(int id)
        {
            try
            {
                var account = db.Accounts.FirstOrDefault(p=>p.ID == id && p.IsActive == true);
                if (account != null)
                {
                    account.password = Tools.GeneratorPassword(7);

                    db.SaveChanges();
                    return true;
                }
                return false;
            }
            catch (Exception)
            {
                throw;
            }
        }

        internal bool Delete(Account deleteObj)
        {
            try
            {
                var account = db.Accounts.FirstOrDefault(p=>p.ID == deleteObj.ID && p.IsActive == true);
                if (account != null)
                {
                    account.IsActive = false;
                    db.SaveChanges();
                    return true;
                }
                return false;
            }
            catch (Exception)
            {
                throw;
            }
        }

        internal bool CheckAccountExisted(Account insertObj)
        {
            return db.Accounts.Any(p=>p.username.Equals(insertObj.username) && p.IsActive == true && p.ID != insertObj.ID);
        }

        internal bool Insert(Account insertObj)
        {
            try
            {
                db.Accounts.Add(insertObj);
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                throw;
            }
        }

        internal Account GetAccountByID(int id)
        {
            return db.Accounts.FirstOrDefault(p=>p.ID == id && p.IsActive == true);
        }

        internal bool Update(Account updateObj)
        {
            try
            {
                var account = db.Accounts.FirstOrDefault(p=>p.ID == updateObj.ID && p.IsActive == true);
                if (account != null)
                {
                    account.password = updateObj.password;
                    account.UserTypeID = updateObj.UserTypeID;

                    db.SaveChanges();
                    return true;
                }
                return false;
            }
            catch (Exception)
            {
                throw;
            }
        }

        internal bool CheckPasswordRight(string oldPass, string username)
        {
            return db.Accounts.Any(p => p.IsActive == true && p.username == username && p.password == oldPass);
        }

        internal bool ChangePass(string newPass, string username)
        {
            try
            {
                var account = db.Accounts.FirstOrDefault(p => p.username.Equals(username) && p.IsActive == true);
                if (account != null)
                {
                    account.password = newPass;
                    db.SaveChanges();
                    return true;
                }
                return false;
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}